package com.bjpowernode.money.service;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 *   注册页面：短信验证码存储在Redis中和在Redis中获取
 */
@Service(interfaceClass = RedisService.class,version = "1.0.0",timeout = 20000)
@Component
public class RedisServiceImpl implements RedisService {

    @Autowired
    RedisTemplate redisTemplate;

    //将短信验证码存储在Redis中
    @Override
    public void push(String phone, String code) {

        //设置redisTemplate对象key的序列化方式  使在存存储到redis中时名字
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //将短信验证码村存储在Redis中
        redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

    }

    //注册页面 将短信验证码从Redis中取出
    @Override
    public String pop(String phone) {

        String code = (String) redisTemplate.opsForValue().get(phone);
        return code;
    }
}
